﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandlingProcedures.DataBase;

namespace HandlingProcedures
{
    /// <summary>
    /// Interaction logic for WindowAddingProcedures.xaml
    /// </summary>
    public partial class WindowAddingProcedures : Window
    {
        private MedicalCard card;

        public WindowAddingProcedures(MedicalCard card)
        {
            InitializeComponent();
            this.card = card;
            ClientInformation infoAboutClient = App.DataBase.GetInformationAboutClient(card.Client.Id);
            if (infoAboutClient != null)
            {
                calendar.DisplayDateStart = infoAboutClient.CheckIn;
                calendar.DisplayDateEnd = infoAboutClient.CheckOut;
            }
            NameOfClient.Content = card.Client.ToString();
        }

        //Загрузка и отображение списка доступных процедур
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBoxProcesures.ItemsSource = App.DataBase.LoadProcedures();
        }

        //загрузка графика процедур при выборе даты
        private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBoxProcesures.SelectedIndex != -1)
            {
                string procedure = ComboBoxProcesures.SelectedItem.ToString();
                dataGridFreeRooms.ItemsSource = App.DataBase.LoadFreeRoomsForProcedure(ComboBoxProcesures.SelectedItem.ToString(), (DateTime)calendar.SelectedDate);
            }
        }

        //загрузка графика процедур при выборе процедуры
        private void ComboBoxProcesures_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(calendar.SelectedDate != null)
            {
                dataGridFreeRooms.ItemsSource = App.DataBase.LoadFreeRoomsForProcedure(ComboBoxProcesures.SelectedItem.ToString(), (DateTime)calendar.SelectedDate);
            }
        }

        //Добавление выбранной процедуры в график
        private void AddProcedureInSchedule_Click(object sender, RoutedEventArgs e)
        {
            if(dataGridFreeRooms.SelectedIndex == -1)
            {
                MessageBox.Show("Выберите процедуру из списка");
            }
            else
            {
                MedicalProcedure procedure = (MedicalProcedure)dataGridFreeRooms.SelectedItem;
                procedure.Name = ComboBoxProcesures.SelectedItem.ToString();
                procedure.Date = (DateTime)(calendar.SelectedDate);
                card.AddProcedure(procedure);
                App.MainWindow.UpdateProcedures();
            }
        }

        //Закрытие окна
        private void CloseWindow_Click(object sender, RoutedEventArgs e)
        {
            App.DataBase.SaveNewProceduresForClient(card.Client.Id, (from pr in card.Procedures where (pr.InRepository == false) select pr));
            this.Close();
        }
    }
}